package com.hujf.mapreduce.join;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * @author Hujf
 * @title: JoinReducer
 * @date 2021/4/314:36
 * @description: TODO
 */
public class JoinReducer extends Reducer<Text,TableBean,TableBean, NullWritable> {
    @Override
    public void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {
        ArrayList <TableBean>arrayList = new ArrayList<TableBean>();
        String name =null;
        for (TableBean value : values) {
            if("order".equals(value.getFlag())){
                TableBean bean = new TableBean();

                try {
                    BeanUtils.copyProperties(bean,value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                arrayList.add(bean);
            }else {
                name = value.getPname();
            }
        }
        for(TableBean bean:arrayList){
            bean.setPname(name);
            context.write(bean,NullWritable.get());
        }
    }
}
